from django.db import models

from utils.modelMixin import modelFiled


class ProtocolApiModel(models.Model):
    name = models.CharField(
        max_length=100,
        verbose_name="API名称",
        unique=True,
        help_text="API 名称",
        db_index=True,  # 建立索引，提高查询效率
    )

    method = modelFiled.DynamicChoicesField(
        max_length=10,
        verbose_name="请求方法",
        help_text="接口方法",
        blank=True,
        choices_getter="get_method_choices",  # 子类实现此方法返回方法列表
    )

    class Meta:
        abstract = True

    @classmethod
    def get_method_choices(cls):
        """
        子类必须实现此方法
        :return: 方法列表
        """
        raise NotImplementedError("Subclasses must implement get_method_choices()")


class HttpApiModel(ProtocolApiModel):
    """Http 协议接口"""

    url = models.URLField(verbose_name="URL", help_text="API 所需 URL")
    request_headers = models.JSONField(verbose_name="请求头", blank=True)
    content_type = models.CharField(
        max_length=32,
        verbose_name="报文类型",
        choices=(("application/json", "application/json"),),
        default="application/json",
        blank=True,
    )
    request_body = models.JSONField(verbose_name="请求体", blank=True)
    response_body = models.JSONField(verbose_name="返回体", blank=True)
    verify_cert = models.BooleanField(
        default=False, verbose_name="是否验证", help_text="控制是否验证SSL证书"
    )
    # cert_file = models.FileField(
    #     upload_to='cases/cert/certificates/',
    #     verbose_name='SSL证地址',
    #     help_text='ssl证书验证',
    #     null=True,
    #     blank=True
    # )

    class Meta:
        db_table = "cool_http_api"
        verbose_name = "HTTP PROTOCOL API"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    @classmethod
    def get_method_choices(cls):
        return ("GET", "GET"), ("POST", "POST"), ("PUT", "PUT"), ("DELETE", "DELETE")


class ThriftApiModel(ProtocolApiModel):
    """RPC 协议接口"""

    class Meta:
        db_table = "cool_thrift_api"
        verbose_name = "RPC PROTOCOL API"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    @classmethod
    def get_method_choices(cls):
        return ("CALL", "CALL"), ("ONeway", "ONeway"), ("ONEWAY", "ONEWAY_NO_REPLY")


class SqlApiModel(ProtocolApiModel):
    """Sql 协议接口"""

    sql = models.TextField(verbose_name="SQL", help_text="SQL 语句")
    size = models.IntegerField(
        verbose_name="数据限制", help_text="limit nums of sql result", blank=True, default=0
    )

    class Meta:
        db_table = "cool_sql_api"
        verbose_name = "SQL PROTOCOL API"

    @classmethod
    def get_method_choices(cls):
        return ("FETCHONE", "FETCHONE"), ("FETCHMANY", "FETCHMANY")


class DubboApiModel(models.Model):
    """Dubbo API模型"""

    name = models.CharField(max_length=100, verbose_name="API名称")
    group = models.CharField(max_length=50, verbose_name="所属组")
    version = models.CharField(max_length=50, verbose_name="版本")
    description = models.CharField(max_length=200, verbose_name="描述")
    method = models.CharField(max_length=100, verbose_name="方法")
    class_name = models.CharField(max_length=200, verbose_name="类")
    retries = models.IntegerField(verbose_name="重试次数")
    timeout = models.IntegerField(verbose_name="超时")
    retries_delay = models.IntegerField(verbose_name="重试间隔")
    retries_max_delay = models.IntegerField(verbose_name="最大重试间隔")
    parameters = models.JSONField(verbose_name="参数")
    request_mapping = models.JSONField(verbose_name="请求映射")
    response_mapping = models.JSONField(verbose_name="返回映射")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        abstract = True
